package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.pojo.user.Address;
import com.qingcheng.service.order.CartService;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.user.AddressService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Reference
    private CartService cartService;

    /**
     * 查找购物车
     * @return
     */
    @GetMapping("/findCartList")
    public List<Map<String,Object>> findCartList() {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName(); //获得当前用户名
        List<Map<String, Object>> cartList = cartService.findCartList(userName);
        return cartList;
    }

    /**
     * 添加商品或删除商品
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/addItem")
    public Result addItem(String skuId,Integer num) {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.addItem(userName,skuId,num);
        return new Result();
    }

    /**
     * 商品页面点击加入购物车,跳转到购物车页面
     * @param skuId
     * @param num
     * @param response
     * @throws IOException
     */
    @GetMapping("/buy")
    public void buy(String skuId, Integer num, HttpServletResponse response) throws IOException {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.addItem(userName,skuId,num);
        response.sendRedirect("/cart.html");
    }

    /**
     * 更改商品选中状态
     * @param skuId
     * @param checked
     * @return
     */
    @GetMapping("/updateChecked")
    public Result updateChecked(String skuId,boolean checked) {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.updateChecked(userName,skuId,checked);
        return new Result();
    }

    /**
     * 删除选中项
     * @return
     */
    @GetMapping("/deleteCheckedCart")
    public Result deleteCheckedCart() {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.deleteCheckedCart(userName);
        return new Result();
    }

    /**
     * 设置优惠价格
     * @return
     */
    @GetMapping("/preferential")
    public Map preferential() {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        int preferential = cartService.preferential(userName);
        Map map = new HashMap();
        map.put("preferential",preferential);
        return map;
    }

    /**
     * 重新查找购物车
     * @return
     */
    @GetMapping("/findNewCartList")
    public List<Map<String,Object>> findNewCartList() {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        List<Map<String, Object>> cartList = cartService.findNewCartList(userName);
        return cartList;
    }

    @Reference
    private AddressService addressService;

    /**
     * 查找收货地址
     * @return
     */
    @GetMapping("/findAddressByUserName")
    public List<Address> findAddressByUserName() {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        List<Address> addressList = addressService.findAddressByUserName(userName);
        return addressList;
    }

    @Reference
    private OrderService orderService;

    /**
     * 保存订单
     * @param order
     * @return
     */
    @PostMapping("/saveOrder")
    public Map<String,Object> saveOrder(@RequestBody Order order) {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        order.setUsername(userName);
        return orderService.add(order);
    }
}
